﻿Imports Commander_Wars.Spieletools
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
''' <summary>
''' Ein beliebig Verschiebbarer Regler. 
''' Hinweis: Rechteck oder Bildgröße verweisen nicht auf das Bild sondern auf die Ausdehnung des Reglers
''' Hinweis:Zahlenwerte sind aus dem Bild entnommen und dürfen nicht verändert werden.
''' </summary>
''' <remarks></remarks>
Public Class ObjektValueSchieber
    Inherits Objekt2D
    Dim Aktiv As Boolean = False
    Dim Buttonaktiv As Integer = 0
    Dim ReglerPosition As Point
    Dim MinValue, Maxvalue As Integer
    Dim Valuen As Integer
    Public Property Value() As Integer
        Get
            Return Valuen
        End Get
        Set(ByVal value As Integer)
            Valuen += value
            If Valuen < MinValue Then Valuen = MinValue
            If Valuen > Maxvalue Then Valuen = Maxvalue
        End Set
    End Property
    Public Sub SetValue(ByVal Wert As Integer)
        Valuen = Wert
        If Valuen < MinValue Then Valuen = MinValue
        If Valuen > Maxvalue Then Valuen = Maxvalue
    End Sub
    Dim Steps As Integer
    Dim Zeichen As String
    Dim ValueBreite As Integer
    ''' <summary>
    ''' Erschafft einen neuen Regler
    ''' </summary>
    ''' <param name="Bild">Der Bildname im Ordner Objekte</param>
    ''' <param name="MinValue"></param>
    ''' <param name="MaxValue"></param>
    ''' <param name="Breite"></param>
    ''' <param name="Position"></param>
    ''' <param name="Startvalue"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal Bild As String, ByVal MinValue As Integer, ByVal MaxValue As Integer, ByVal Breite As Integer, ByVal Position As Point, ByVal Startvalue As Integer, ByVal Fixed As PointF, Optional ByVal Frame As Integer = 5, Optional ByVal Steps As Integer = 1, Optional ByVal Zeichen As String = "", Optional ByVal Handler As ObjektPanel = Nothing)
        MyBase.New(Bild, New Rectangle(0, 0, 52, 17), Position, Fixed, Handler)
        Me.MinValue = MinValue
        Me.Maxvalue = MaxValue
        Valuen = Startvalue
        ValueBreite = Breite
        If Valuen < MinValue Then Valuen = MinValue
        If Valuen > MaxValue Then Valuen = MaxValue
        Me.Steps = Steps
        If Me.Steps < 1 Then Me.Steps = 1
        MyBase.Frame = New Point(Frame, 0)
        Me.Zeichen = Zeichen

    End Sub

    Public Overrides Function Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        Return False
    End Function

    Public Overrides Function KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        Return False
    End Function

    Public Overloads Overrides Function MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If (Not Objekt2D.AlreadyKlickt And Not HasFocus) Or Focused Then
            Dim P As Point = GetRealKoordinaten()
            If New Rectangle(P.X, P.Y, ValueBreite + 22, Rechteck.Height).Contains(e.X, e.Y) Then
                If New Rectangle(Koordinaten.X, Koordinaten.Y, 11, 17).Contains(e.X, e.Y) Then
                    Buttonaktiv = -1
                ElseIf New Rectangle(P.X + ValueBreite + 11, P.Y, 11, 17).Contains(e.X, e.Y) Then
                    Buttonaktiv = 1
                Else
                    Aktiv = True
                End If
                Objekt2D.AlreadyKlickt = True
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Public Overloads Overrides Function MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If (Not Objekt2D.AlreadyKlickt And Not HasFocus) Or Focused Then
            Dim P As Point = GetRealKoordinaten()
            If New Rectangle(P.X, P.Y, Rechteck.Width, Rechteck.Height).Contains(e.X, e.Y) Then
                Objekt2D.AlreadyKlickt = True
                Return True
            Else
                Return False
            End If
        End If
    End Function

    Public Overloads Overrides Function MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As String
        Aktiv = False
        Buttonaktiv = 0
        MyBase.Frame = New Point(0, 0)
        Return ""
    End Function

    Public Sub MouseMove(ByVal X As Integer, ByVal Y As Integer)
        If Aktiv = True Then
            Dim P As Point = GetRealKoordinaten()
            If X < P.X + 11 Then
                Valuen = MinValue
            ElseIf X > P.X + ValueBreite Then
                Valuen = Maxvalue
            Else
                Valuen = (X - P.X - 11) * (Maxvalue - MinValue) / (ValueBreite - 15) + MinValue
            End If
            If Valuen < MinValue Then Valuen = MinValue
            If Valuen > Maxvalue Then Valuen = Maxvalue
        End If
    End Sub

    Public Overrides Sub Malen()
        Dim sngRotation As Double = 0
        Dim intColor As Color = Color.FromArgb(255, 255, 255, 255)
        Grafikengine.ptfRotCenter = New Point(0, 0)

        With Grafikengine.recBild
            .X = 0 : .Width = 11
            .Y = 0 : .Height = 17
        End With
        Dim P As Point = GetRealKoordinaten()
        Grafikengine.ptfPosition = P
        With Grafikengine.szfScale
            .Height = 17
            .Width = 11
        End With
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        With Grafikengine.recBild
            .X = Rechteck.Width - 11 : .Width = 11
            .Y = 0 : .Height = 17
        End With
        Grafikengine.ptfPosition = New Point(P.X + ValueBreite + 11, P.Y)
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        'Wir finden heraus wie oft wir den Mittelteilmalen müssen
        Dim Breite As Integer = Divi(ValueBreite, 15)
        'Wir malen alle Mittelteile bis auf den letzten
        With Grafikengine.recBild
            .X = 26 : .Width = 15
            .Y = 0 : .Height = 17
        End With
        With Grafikengine.szfScale
            .Height = 17
            .Width = 15
        End With
        Grafikengine.ptfPosition.X = P.X + 11
        For I = 0 To Breite - 2
            Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
            Grafikengine.ptfPosition.X += 15
        Next
        'Der letzte wird jetzt gemalt, weil er eine Ausnahme ist.
        Grafikengine.ptfPosition.X = P.X + 11 + ValueBreite - 15
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        'So jetzt müssen wir noch den Regler malen
        'So das wäre der Nullwert des Reglers
        Grafikengine.ptfPosition.X = P.X + 11
        'Jetzt addieren wir seine Verschiebung drauf.
        Grafikengine.ptfPosition.X += (Value - MinValue) / (Maxvalue - MinValue) * (ValueBreite - 15)
        With Grafikengine.recBild
            .X = 11 : .Width = 15
            .Y = 0 : .Height = 17
        End With
        With Grafikengine.szfScale
            .Height = 15
            .Width = 17
        End With
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        'Wir malen den Aktuellen Wert noch
        Dim T As New ObjektText(New String() {Value.ToString + Zeichen}, P.X + ValueBreite / 2, P.Y, New Color() {Color.Black}, "Arial", 10, FontStyle.Regular)
        Grafikengine.Sprite.End()
        Schriftengine.Sprite2.Begin(SpriteFlags.AlphaBlend)
        T.Malen(Schriftengine.Sprite2)
        Schriftengine.Sprite2.Flush()
        Schriftengine.Sprite2.End()
        Grafikengine.Sprite.Begin(SpriteFlags.AlphaBlend)


        'Zum Schluss machen wir die Frames
        Frames()


    End Sub
    Private Sub Frames()
        If Buttonaktiv <> 0 Then
            Frame = New Point(Frame.X + 1, 0)
            Value = Steps * Buttonaktiv
        End If
    End Sub
End Class
